package mysql

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"teacher1/appconfig"
)

var (
	DB  *gorm.DB
	err error
)

func MysqlInit() {
	appConfig := appconfig.AppConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", appConfig.User, appConfig.Password, appConfig.Host, appConfig.Port, appConfig.Database)
	DB, err = gorm.Open("mysql", dsn)
	if err != nil {
		panic(err)
	} else {
		fmt.Println("数据库连接成功")
	}
	// 自动迁移
	DB.AutoMigrate(&Teacher{}, &Student{})

}
